package com.api.user.enums;

import org.apache.commons.lang3.StringUtils;

/**
 * @author RenQiang
 * @date 2021/7/13
 */
public enum GameEnum {
    /**
     * PUBG
     */
    PUBG("PUBG"),
    /**
     * DOTA2
     */
    DOTA2("DOTA", "2"),
    /**
     * CS GO
     */
    CS_GO("CS", "GO");

    private final String[] keywords;

    GameEnum(String... keywords) {
        this.keywords = keywords;
    }

    public String[] getKeywords() {
        return this.keywords;
    }

    /**
     * 根据玩家数量计算队伍数量
     *
     * @param players
     * @return
     */
    public int calcTeamCount(int players) {
        return this == GameEnum.PUBG ? players / 4 : 2;
    }

    /**
     * 根据玩家数量计算每队人数
     *
     * @param players
     * @return
     */
    public int calcPlayerPerTeam(int players) {
        return this == GameEnum.PUBG ? 4 : players / 2;
    }

    public static GameEnum valueOfGameName(String gameName) {
        if (StringUtils.isBlank(gameName)) {
            return null;
        }
        for (GameEnum value : GameEnum.values()) {
            boolean fail = false;
            for (String keyword : value.keywords) {
                if (!StringUtils.containsIgnoreCase(gameName, keyword)) {
                    fail = true;
                    break;
                }
            }
            if (!fail) {
                return value;
            }
        }
        return null;
    }
}
